package com.dd.cloud.user.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.AdminConstants;
import com.dd.cloud.common.constants.MechanConstants;
import com.dd.cloud.user.entity.admin.AdminSetDep;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.admin.AdminSetDepMapper;
import com.dd.cloud.user.req.admin.AdminSetDepReq;
import com.dd.cloud.user.service.admin.IAdminSetDepService;
import com.dd.cloud.user.service.role.IAdminPowerService;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.exception.ZekeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理员能设置的科室表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-08-02
 */
@Service
public class AdminSetDepServiceImpl extends ServiceImpl<AdminSetDepMapper, AdminSetDep> implements IAdminSetDepService {
    @Autowired
    private IAdminPowerService adminPowerService;

    /**
     * 获取当前登录人能设置的科室
     * @return
     */
    @Override
    public List<AdminSetDep> getAdminSetDepList() {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        QueryWrapper<AdminSetDep> qw = new QueryWrapper<>();
        int flag = adminPowerService.queryRoleIsAdmin(loginInfo.getId(), LoginInfo.UserType.ADMIN);
        if (flag == AdminConstants.Admin.ADMIN){
            qw.lambda().eq(AdminSetDep::getAdminId, 0)
                    .eq(AdminSetDep::getStatus, AdminConstants.SetDepStatus.NORMAL);
        } else {
            qw.lambda().eq(AdminSetDep::getAdminId, loginInfo.getId())
                    .eq(AdminSetDep::getStatus, AdminConstants.SetDepStatus.NORMAL);
        }
        List<AdminSetDep> list = list(qw);
        return list;
    }

    /**
     * 批量添加能配置的科室
     * @param depList
     */
    @Override
    public void addSetDepBatch(List<AdminSetDepReq> depList) {
        if (depList.isEmpty()){
            return;
        }
        LinkedList<AdminSetDep> ll = new LinkedList<>();
        UpdateWrapper<AdminSetDep> uw = new UpdateWrapper<>();
        uw.lambda().eq(AdminSetDep::getAdminId, depList.get(0).getAdminId())
                .set(AdminSetDep::getStatus, AdminConstants.SetDepStatus.DEL);
        update(uw);
        depList.forEach( d ->{
            AdminSetDep dep = new AdminSetDep();
            BeanUtils.copyProperties(d, dep);
            dep.setAdminId(d.getAdminId());
            dep.setStatus(AdminConstants.SetDepStatus.NORMAL);
            dep.setCreateDate(LocalDateTime.now());
            ll.add(dep);
        });
        int flag = getBaseMapper().insertBatchSomeColumn(ll);
        if (flag <= 0){
            throw new ZekeException(500, "配置科室添加失败");
        }
    }

    /**
     * 查询当前管理员能配置的科室
     * @return
     */
    @Override
    public List<String> getAdminSetDep() {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        QueryWrapper<AdminSetDep> qw = new QueryWrapper<>();
        qw.lambda().eq(AdminSetDep::getStatus, MechanConstants.MechanSetDepStatus.NORMAL)
                .eq(AdminSetDep::getAdminId, loginInfo.getId());
        List<AdminSetDep> list = list(qw);
        return list.stream().map(AdminSetDep::getKeshiId).collect(Collectors.toList());
    }
}
